<?php

require_once('./fpdf/tcpdf/tcpdf.php');

// create new PDF document
//$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new TCPDF($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);


$pdf->SetHeaderData("", "", "ใบผ่านรายการมาตราฐาน 2", "");

// set header and footer fonts
//$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setHeaderFont(Array("thsarabunb", 'B', 22));

$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
// ---------------------------------------------------------
// set font
//$pdf->SetFont('helvetica', 'B', 20);
$pdf->SetFont('thsarabunb', 'B', 22);

// add a page
$pdf->AddPage();

//$pdf->Write(0, 'รายงานสมุดเงินสดจ่าย', '', 0, 'L', true, 0, false, false, 0);
//$pdf->SetFont('helvetica', '', 8);
// -----------------------------------------------------------------------------


$pdf->SetFont('thsarabunb', 'B', 16);



require_once("includes/connect.php");
require_once("includes/function.php");

//$from = $_REQUEST["from"];
//$to = $_REQUEST["to"];
//$bank_name = $_REQUEST["bank_name"];
//$branch_name = $_REQUEST["branch_name"];
//$ex1_id = $_REQUEST["ex1_id"];


$db = new myDBS();
$db->selectDB("account");
$sql = "SELECT expenses2_name, e_id, sum(net) AS net "
        . " FROM debit "
        . " group by expenses2_name, e_id";

//echo $sql;
$result = $db->runSQL($sql);

$tbl_header = '
<table cellspacing="0" cellpadding="1" border="1" width="100%">
<tr>
    <td>รายการ</td>
    <td>รหัสบัญชี</td>
    <td>เดบิต</td>
    <td>เครดิต</td>
</tr>
';
while ($rows = $db->fetch_object($result)) {

    //$date = date_create($rows1->date_create);
    $tbl_body = $tbl_body . '
                    <tr>
                        <td>' . $rows->expenses2_name . '</td>
                        <td align="center">' . $rows->e_id . '</td>
                        <td align="right">' . number_format($rows->net, 2) . '</td>
                        <td align="right">' .  '</td>
                    </tr>
                    ';
}

$sql = "SELECT * "
        . " FROM credit ";
//        . " group by expenses2_name, e_id";

//echo $sql;
$result = $db->runSQL($sql);
while ($rows = $db->fetch_object($result)) {

    //$date = date_create($rows1->date_create);
    $tbl_body = $tbl_body . '
                    <tr>
                        <td>เงินฝากธนาคาร - ' . $rows->bank_type . '</td>
                        <td align="center">110201</td>
                        <td align="right">' .  '</td>
                        <td align="right">' . number_format($rows->net, 2) . '</td>
                    </tr>
                    ';
}

$tbl_footer = '</table>';

$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');
$pdf->Output();
?>